//
//  CCTabBarController.swift
//  SinaWebo
//
//  Created by 陶建 on 16/3/22.
//  Copyright © 2016年 taoJ. All rights reserved.
//

import UIKit
import SnapKit

class CCTabBarController: UITabBarController {
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        
        // tabBarItem个数
        let tabBarItemCount: CGFloat = CGFloat(tabBar.items!.count)
        
        // 添加 中间的按钮
        self.tabBar.addSubview(centerButton)
        // 设置中间按钮的位置
        let width = tabBar.frame.size.width / tabBarItemCount
        let height = tabBar.frame.size.height
        centerButton.bounds = CGRect(x: 0, y: 0, width: width, height: height)
        centerButton.center = CGPoint(x: tabBar.frame.size.width * 0.5, y: tabBar.frame.size.height * 0.5)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置UITabBar / UINavigationBar样式
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        // 动态加载显示的界面: 读取本地文件里面的类名, 相当于从服务器返回的数据
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        do{
            // 序列化JSONData
            let dictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
            // 遍历dictionary 中的字典
            for dict:[String:String] in dictionary as! [Dictionary] {
                let childControllerName = (NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"]! as! String) + "." + dict["vcName"]!
                setUpAddChildViewController(childControllerName, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        }
        catch{
            // 如果服务器返回没有结果我们就这么写就可以了
            setUpAddChildViewController(CCHomeTableViewController(), title: "首页", imageName: "tabbar_home")
            setUpAddChildViewController(CCMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
            setUpAddChildViewController(CCViewController(), title: "clearColorImage", imageName: "")
            setUpAddChildViewController(CCDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
            setUpAddChildViewController(CCProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        }
    }
    
    // 根据字符添加控制器
    private func setUpAddChildViewController(childControllerName: String, title: String, imageName: String) {
        // 转换成类
        let cls:AnyClass = NSClassFromString(childControllerName)!
        let classType = cls as! UIViewController.Type
        // 初始化类相当于oc 从 metaclass -> class 
        let childController = classType.init()
        setUpAddChildViewController(childController, title: title,imageName: imageName)
    }
    
    // 根据类添加控制器
    private func setUpAddChildViewController(childController: UIViewController, title: String, imageName: String) {
        childController.tabBarItem.title = title
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        let selectedImageName = imageName + "_highlighted"
        childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)
        let navVC = UINavigationController(rootViewController: childController)
        addChildViewController(navVC)
    }
    
    private lazy var centerButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(CCTabBarController.tapCenterButton(_:)), forControlEvents: .TouchUpInside)
        return btn
    }()
    
    func tapCenterButton(button: UIButton) {
        print("tapCenterButton")
    }
}

// 中间占位的类
class CCViewController: UIViewController {
    
}
